﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace JobSeek.Classes
{
    public class MyRoleProvider : RoleProvider
    {

        private string pApplicationName = "DamienAndVictorJobSeekerApplication";

        /// <summary>
        /// Add user to the role. Checking of foreign key is not done in database. Checking from code is necessary.
        /// </summary>
        /// <param name="usernames"></param>
        /// <param name="roleNames"></param>
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            //the object will automatically dispose itself when 'using' is used.
            using (job_seekerEntities ctx = new job_seekerEntities())
            {
                foreach (string role in roleNames)
                {
                    foreach (string username in usernames)
                    {
                        UsersInRole userInRole = new UsersInRole();
                        userInRole.roleName = role;
                        userInRole.login_credential = username;

                        ctx.AddToUsersInRoles(userInRole);
                        ctx.SaveChanges();
                    }
                }
            }
        }



        public override string ApplicationName
        {
            get
            {
                return pApplicationName;
            }
            set
            {
                pApplicationName = value;
            }
        }

        public override void CreateRole(string roleName)
        {
            using (job_seekerEntities ctx = new job_seekerEntities())
            {
                var find_role = from role in ctx.Roles
                                where role.roleName == roleName
                                select role;

                if (find_role.Count() == 0)
                {
                    Role role = new Role();
                    role.applicationName = this.ApplicationName;
                    role.roleName = roleName;

                    ctx.AddToRoles(role);
                    ctx.SaveChanges();
                }
                else
                {
                    //Role exist;
                }
            }
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            using (job_seekerEntities ctx = new job_seekerEntities())
            {
                var _role = (from role in ctx.Roles
                             where role.roleName == roleName
                             select role).SingleOrDefault<Role>();

                ctx.Roles.DeleteObject(_role);

                ctx.SaveChanges();
                return true;
            }
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string username)
        {
            using (job_seekerEntities ctx = new job_seekerEntities())
            {
                var result = (from x in ctx.UsersInRoles
                              where x.login_credential == username
                              select new
                              {
                                  x.roleName
                              });

                if (result.Count() > 0)
                {

                    string[] results = new string[result.Count()];
                    int i = 0;
                    foreach (var s in result)
                    {
                        results[i] = s.roleName;
                        i++;
                    }

                    return results;
                }
                else
                {
                    return null;
                }
            }
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            
            using (job_seekerEntities ctx = new job_seekerEntities())
            {
                var result = from x in ctx.UsersInRoles
                             where x.login_credential == username
                             where x.roleName == roleName
                             select x;

                if (result.Count() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
                             
            }
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            throw new NotImplementedException();
        }
       
    }
}